# Create your views here.
import json

from django.http import JsonResponse
from django.utils.decorators import method_decorator
from django.views import View
from django.views.decorators.csrf import csrf_exempt

course_dict = {
    'name': '课程名称',
    'introduction': '课程介绍',
    'price': 0.21,
}


# 1 Django FBV 编写接口
@csrf_exempt  # 取消csrf限制
def course_list(request):
    if request.method == 'GET':
        # return HttpResponse(json.dumps(course), content_type='application/json') # 同下效果一致
        return JsonResponse(course_dict)

    if request.method == 'POST':
        course = json.load(request.body.decode('utf8'))
        return JsonResponse(course, safe=False)


# 2 Django CBV 编写接口
@method_decorator(csrf_exempt, name='dispatch')
class CourseList(View):
    def get(self, request):
        return JsonResponse(course_dict)

    def post(self, request):
        course = json.load(request.body.decode('utf8'))
        return JsonResponse(course, safe=False)

# 原生的 需要自己写功能
# 分页 排序 认证 权限 限流


